<?php

namespace app\models;

use app\librarys\DbCacheUtility;

/**
 * 管理员权限功能角色
 * Class Role
 * @package app\models
 */
class Role
{
    public static function getCount()
    {
        $cmd = \Yii::$app->db->createCommand('SELECT COUNT(1) FROM t_role');
        $count = $cmd->queryScalar();
        return $count;
    }

    /**
     * 获取角色列表
     * @param int $offset 分页查询起始位置
     * @param int $limit 分页查询每页数量
     * @return array
     */
    public static function getList($offset, $limit)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_role ORDER BY create_datetime DESC LIMIT :offset, :limit');
        $cmd->bindValue(':offset', $offset);
        $cmd->bindValue(':limit', $limit);

        $items = $cmd->queryAll();

        return $items;
    }

    /**
     * 获取所有启用的角色列表(不分页)
     * @return array
     */
    public static function getAllEnabledList()
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_role WHERE is_enabled=1 ORDER BY create_datetime DESC');
        $items = DbCacheUtility::queryAll($cmd);
        return $items;
    }

    public static function getDetail($roleId)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_role WHERE id=:roleId');
        $cmd->bindValue(':roleId', $roleId);
        $item = $cmd->queryOne();
        if ($item == false) {
            return null;
        }

        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_role_function_point WHERE role_id=:roleId');
        $cmd->bindValue(':roleId', $roleId);
        $items = $cmd->queryAll();
        $item['role_function_point_list'] = $items;

        return $item;
    }
}